package com.lnking.lnkingplace.Util;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.model.dto.user.UserDto;
import com.lnking.lnkingplace.model.entity.User;
import com.lnking.redis.server.RedisServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;


/**
 * @author 联坤
 */
@Component
public class UserUtil {

    private  static String NULL="null";
    @Autowired
    RedisServer redisServer;

    public UserUtil() {
    }

    public UserDto getUser(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if (StringUtils.isNotBlank(token)&&!NULL.equals(token)) {
            Object value = redisServer.get(token);
            this.redisServer.get(token);
            String tmpStr = value == null ? null: value.toString();
            Assert.notNull(tmpStr,"登录过期,请重新登录");
            return (UserDto) JSON.parseObject(tmpStr, UserDto.class);
        } else {
            return null;
        }
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

}
